package com.oocl.aeat.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.oocl.aeat.dao.MerchantDao;
import com.oocl.aeat.dao.impl.MerchantDaoImpl;
import com.oocl.aeat.dto.MerchantDto;
import com.oocl.aeat.pojo.Merchant;
import com.oocl.aeat.redis.MerchantCache;
import com.oocl.aeat.service.MerchantService;

public class MerchantServiceImpl implements MerchantService {
	private MerchantDao merchantDao;
	public MerchantServiceImpl() {
		merchantDao = new MerchantDaoImpl();
	}
	
	@Override
	public boolean addMerchant(Merchant merchant) {
		if(merchantDao.addMerchant(merchant)>0){
			//更新缓存
			MerchantDto dto = convert(merchant);
			MerchantCache.add(dto);
			return true;
		}
		return false;
	}

	@Override
	public boolean deleteMerchant(String id) {
		if(merchantDao.deleteMerchant(id)>0){
			//更新缓存
			MerchantCache.delete(id);
			return true;
		}
		return false;
	}

	@Override
	public boolean updateMerchant(Merchant merchant) {
		if(merchantDao.updateMerchant(merchant)>0){
			MerchantDto dto = convert(merchant);
			MerchantCache.update(dto);
			return true;
		}
		return false;
	}

	@Override
	public List<Merchant> queryAllMerchant(Integer block) {
		return merchantDao.queryAllMerchant(block);
	}

	@Override
	public Merchant queryMerchantById(String id) {
		return merchantDao.queryMerchantById(id);
	}
	
	@Override
	public List<MerchantDto> convertList(List<Merchant> merchants) {
    	List<MerchantDto> mdtolist = null;
    	if(merchants!=null){
    		mdtolist = new ArrayList<MerchantDto>();
    		MerchantDto dto = null;
    		for(Merchant m : merchants){
    			dto = new MerchantDto();
    			dto.setId(m.getId());
    			dto.setName(m.getName());
    			dto.setStoreName(m.getStoreName());
    			dto.setImg(m.getImg());
    			dto.setAddress(m.getAddress());
    			mdtolist.add(dto);
    		}
    	}else{
    		mdtolist = new ArrayList<MerchantDto>();
    	}
    	return mdtolist;
	}

	@Override
	public MerchantDto convert(Merchant merchant) {
		MerchantDto dto = null;
    	if(merchant!=null){
    		dto = new MerchantDto();
    		dto.setId(merchant.getId());
    		dto.setName(merchant.getName());
    		dto.setStoreName(merchant.getStoreName());
    		dto.setImg(merchant.getImg());
    		dto.setAddress(merchant.getAddress());
    	}
    	return dto;
	}
}
